<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Crypt;
use Hash;
use DB;

class UserController extends Controller
{
    //用户添加
    public function add()
    {
    	return view('admin.user.add',['title'=>'用户添加']);
    }

    // 用户列表
    public function index(request $request)
    {
        // 查询数据库所有用户的数据
        $row = DB::table('users') -> get();
       
        return view('admin.user.index',['title'=>'用户列表','row'=>$row]);
    }

     // 用户编辑
    public function edit(request $request)
    {
        $id = $request->input('id');
        $row = DB::table('users') -> where('id','=',$id) -> get();

        return view('admin.user.update',['title'=>'用户修改','row'=>$row]);
    }

    // 数据库添加用户
    public function insert(request $request)
    {
    	$this->validate($request,[
			'name' => 'required|min:6|max:18',
            'nickname' => 'required',
			'email' => 'required|email',
			'password' => 'required',
			'repassword' => 'required|same:password'
		],[
			'name.required' => '用户名不能为空',
			'name.min' => '用户名最少为6位',
			'name.max' => '用户名最多为18位',
			'email.required' => '邮箱不能为空',
			'email.email' => '邮箱格式不正确',
			'password.required' => '密码不能为空',
			'repassword.required' => '确认密码不能为空',
			'repassword.same' => '确认密码与原密码不一致'
		]);
    	// 打印所有
    	// dd($request->all());
    	$data = $request -> except('_token','repassword');

    	// // 处理密码字段
    	$data['password'] = Crypt::encrypt($data['password']);
    	// $p = Crypt::decrypt($data['password']);
    	// 哈希加密
    	// $data['password'] = Hash::make($data['password']);
    	// dd($data['password']);
    	// 处理status字段
    	$data['status'] = 0;
    	// 处理token字段
    	$data['remember_token'] = str_random(50);
    	// 处理时间
    	$time = time();
    	$data['created_at'] = $time;
    	$data['updated_at'] = $time;
    	// 处理头像
    	if($request -> hasFile('pic'))
    	{
    		if($request -> file('pic') -> isValid())
    		{
    			$suffix = $request -> file('pic') -> getClientOriginalExtension();
    			$fileName = time().mt_rand(10000,99999).'.'.$suffix;
    			// 移动文件
    			$move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);
    			if($move)
    			{
    				$data['pic'] = $fileName;
    			}
    			else
    			{
    				$data['pic'] = 'default.jpg';
    			}
    		}else
    		{
    			$data['pic'] = 'default.jpg';
    		}
    	}
    	else
    	{
    		$data['pic'] = 'default.jpg';
    	}
    	$res = DB::table('users') -> insert($data);
    	if($res)
    	{
    		return redirect('/admin/user/index');
    	}else
    	{
    		return back();
    	}
    }
    
    // 用户修改
    public function update(request $request)
    {   
        // 获取所有form表单提交的数据
        $data = $request -> except('_token','id');

        // 获取ID
        $id = $request -> input('id');

        // 头像处理
        if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                // 移动文件
                $move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);
                if($move)
                {
                    $data['pic'] = $fileName;
                }
                else
                {
                    $data['pic'] = 'default.jpg';
                }
            }else
            {
                $data['pic'] = 'default.jpg';
            }
        }
        else
        {
            $data['pic'] = 'default.jpg';
        } 

        //更新用户数据
       if( DB::table('users') -> where('id','=',$id) -> update($data) )
       {
            return redirect('/admin/user/index');
       } else {
            return back();
       }
    }

    // 用户的删除
    public function delete(request $request)
    {
        // 获取id
        $id = $request -> input('id');
        
        // 数据库的删除操作
        if( DB::table('users') -> where('id','=',$id) -> delete() )
       {
            return redirect('/admin/user/index');
       } else {
            return back();
       }
    }
}
